package chapter5_stack_and_linked;

import java.util.*;

/**
 * Created by yuh on 2018/4/10.
 */
public class Brackets {


    public static void main(String[] args) {
        String[] strs = new String[]{"{", "[", "{", "}", "]", "}"};
        System.out.println(isLegalBrackets(strs));
    }

    private static boolean isLegalBrackets(String[] arr) {
        LinkedList<String> stack = new LinkedList<>();
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("{", "}");
        hashMap.put("(", ")");
        hashMap.put("[", "]");

        for (String s : arr) {
            if (!hashMap.containsKey(s)) {
                String pop = stack.removeFirst();
                if (!hashMap.get(pop).equals(s)) {
                    return false;
                }
            } else {
                stack.addFirst(s);
            }
        }
        return stack.isEmpty();
    }
}
